# _*_ coding: utf-8 _*_
#!/usr/bin/env python

#      ___________________
#  ___| Modulbeschreibung \_________________________________________________________________________
# | Startet alle Klassen und beendet sie
# | 
# |__________________________________________________________________| Zuständigkeit [hendrik|____


# Codings ändern!!!!!!

import sys, os
import pygame

import menu
import gamemap
import graphic
import player
import sound
import gamelogic
import network
import thread
#import time 																												#!!!

# Aus Portabilitaetsgruenden. Haengt das Arbeitsverzeichnis vor den Dateinamen
def addWorkPath(filename):
	return os.path.join(os.path.dirname(sys.argv[0]), 'data', filename)

class RetroRacer(object):
	def __init__(self):
		self.settings	= {	# Grafikoptionen
							'screen_size':	(500, 300),
							'screen_flags':	0,
							# Framerate 0 für Maximum
							'fps_limit':	25}
		pygame.init()
		pygame.mixer.init()
		pygame.display.set_caption('Retro Racer')
		pygame.mouse.set_visible(False)
		self.screen		= pygame.display.set_mode(self.settings['screen_size'], self.settings['screen_flags'])
		self.clock		= pygame.time.Clock()
		
		self.menu		= menu.Menu(self, Race(self))
		#self.startSinglePlayerGame()
	
	def startGame(self, race):
		'''Startet das Rennen'''
		race.iniciateRace()
		self.quitGame()

	def quitGame(self):
		pygame.quit()
		sys.exit()


class Race(object):
	def __init__(self, game):
		self.game			= game
		self.is_running		= True
		
		self.players		= []
		self.network 		= network.Network()
		self.game_map		= gamemap.Map()
		self.graphic		= graphic.Graphic(self)
		#self.network		= network.Network(self)
		self.game_logic		= gamelogic.GameLogic(self)
		#self.time			= pygame.time.get_ticks()
		self.sound = sound.Sound()
		self.startime		= 0
		self.lock=thread.allocate_lock()
		
		for kart_nr in range(8):
			self.players.append(player.Player(self, kart_nr))
	
	def setPlayerData(self, object, player, data):
		self.lock.acquire()
		self.players[player].object = data
		self.lock.release()
		return True
		
	def connectToServer(self, race, server, port = 50000):
		'''Wird aufgerufen um eine Verbindung zum Server herzustellen'''
		self.network.runClient(race, server, port)
		
	def iniciateRace(self):
		'''Initialisierung des Rennens'''
		#self.network.start()
		self.graphic.showRaceIntro()
			#self.game.clock.tick(self.game.settings['fps_limit'])
		self.game_logic.start()
		self.starttime = pygame.time.get_ticks()
		while self.is_running:
			self.graphic.drawScene()
			#self.time = pygame.time.get_ticks()
			# Begrenzt die Framerate
			self.game.clock.tick(self.game.settings['fps_limit'])
	
	def stopRace(self):
		'''Stoppt alle Threads wenn das Rennen abgeschlossen ist'''
		#!!! Auf Threads warten!!!
		print 'Send kill thread signal, waiting for response...'
		self.game_logic.stopThread()
		#self.game_logic.run_thread = False
		#self.stop_thread.set()
		#self.join(self)
		#self.game_logic.stoped.wait()
		#self.join()
		#self.stoped.set()
		#time.sleep(0.51)
		if self.game_logic.stoped.isSet():
			print 'game-logic stopped'
		#self.network.stop()
		#self.game_logic.stopThread()
		self.is_running = False
		#self.game.quitGame()
	
	#def __del__(self):
	#	self.stopRace()


if __name__ == '__main__':
	main = RetroRacer()
